package cn.autowired.exam.controller;

import cn.autowired.exam.base.CommonResponse;
import cn.autowired.exam.entity.Application;
import cn.autowired.exam.entity.Student;
import cn.autowired.exam.service.ApplicationService;
import cn.autowired.exam.service.StudentPaperService;
import cn.autowired.exam.service.StudentService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 *
 *   @description : Application 控制器
 *   ---------------------------------
 *      @author cq
 *   @since 2018-05-25
 */
@RestController
@RequestMapping("/application")
public class ApplicationController extends AbstractController {
private final Logger logger=LoggerFactory.getLogger(ApplicationController.class);

    @Autowired
    public ApplicationService applicationService;

    @Autowired
    public StudentService studentService;

    @Autowired
    public StudentPaperService studentPaperService;

    /**
     * 学生向指定老师预约心理咨询
     * @param teacherId 老师id
     * @param studentNo 学生号
     * @param time 指定时间
     * @return 是否成功
     */
    @RequestMapping("/apply")
    public ResponseEntity apply(Integer teacherId,String studentNo,String time) throws ParseException {
        Application application = new Application();
        application.setStudentNo(studentNo);
        application.setTeacherId(teacherId);
        //time = 2018-05-25 time8to9
        String[] times = time.split(" ");
        String[] tos = times[1].split("to");
        String parseTime = times[0]+" "+(Integer.parseInt(tos[1])-1)+":00";
        application.setTime(DateUtils.parseDate(parseTime,"yyyy-MM-dd hh:mm"));
        boolean flag = application.insertOrUpdate();
        return ResponseEntity.ok(flag?CommonResponse.ok():CommonResponse.error());
    }

    /**
     * 同意指定的申请信息
     * @param id 申请信息的id
     * @return 是否成功
     */
    @RequestMapping("/approve")
    public ResponseEntity approve(Integer id){
        Application application = applicationService.selectById(id);
        application.setIsRefused(0);
        boolean flag = application.updateById();
        return ResponseEntity.ok(flag?CommonResponse.ok():CommonResponse.error());
    }

    /**
     * 拒绝指定的申请信息
     * @param id 申请信息的id
     * @param reason 拒绝理由
     * @return 是否成功
     */
    @RequestMapping("/refuse")
    public ResponseEntity refuse(Integer id,String reason){
        Application application = applicationService.selectById(id);
        application.setIsRefused(1);
        application.setReason(reason);
        boolean flag = application.updateById();
        return ResponseEntity.ok(flag?CommonResponse.ok():CommonResponse.error());
    }

    /**
     * 根据老师id找到预约列表 并展示出基本信息 供前台展示
     * @param teacherId
     * @return
     */
    @RequestMapping("/getApplicationByTeacherId")
    public ResponseEntity getApplicationByTeacherId(Integer teacherId){
        List<Application> applications = applicationService.selectByTeacherId(teacherId);
        List list = new LinkedList();
        if (applications.size()>0){
            for (Application application : applications) {
                String studentNo = application.getStudentNo();
                Student student = studentService.selectByStudentNo(studentNo);
                HashMap studentInfo = new HashMap();
                studentInfo.put("studentName",student.getName());
                studentInfo.put("studentInfo",student.getMajor()+student.getGradeInfo()+"级"+student.getClassInfo()+"班");
                studentInfo.put("application",application);
                studentInfo.put("applicationTime",DateFormatUtils.format(application.getTime(),"yyyy-MM-dd hh:mm"));
                list.add(studentInfo);
            }
        }
        return ResponseEntity.ok(CommonResponse.ok(list));
    }

    /**
     * 根据老师id和date扎到预约信息列表
     * @param teacherId
     * @param date
     * @return
     */
    @RequestMapping("/getApplicationByTeacherIdAndDate")
    public ResponseEntity getApplicationByTeacherIdAndDate(Integer teacherId,String date){
        List<Application> applications = applicationService.selectByTeacherIdAndDate(teacherId,date);
        List list = new LinkedList();
        if (applications.size()>0){
            for (Application application : applications) {
                String studentNo = application.getStudentNo();
                Student student = studentService.selectByStudentNo(studentNo);
                HashMap studentInfo = new HashMap();
                studentInfo.put("studentName",student.getName());
                studentInfo.put("studentInfo",student.getMajor()+student.getGradeInfo()+"级"+student.getClassInfo()+"班");
                studentInfo.put("application",application);
                list.add(studentInfo);
            }
        }
        return ResponseEntity.ok(CommonResponse.ok(list));
    }

    /**
     * 根据学号找到预约信息列表
     * @param studentNo
     * @return
     */
    @RequestMapping("/getApplicationByStudentNo")
    public ResponseEntity getApplicationByStudentNo(String studentNo){
        List<Application> applications = applicationService.selectByStudentNo(studentNo);
        return ResponseEntity.ok(CommonResponse.ok(applications));
    }
}